package com.libgdxbar.pvzlikegame.entities;

import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.libgdxbar.pvzlikegame.screens.GameScreen;
import com.libgdxbar.pvzlikegame.screens.ScreenManager;

import org.libgdxbar.projects.interfaces.IInput;
import org.libgdxbar.projects.interfaces.callback.InputCallback;

// 可点击实体基类 请实现 onClick 方法以编写点击事件逻辑
public abstract class RectTouchableEntity extends RectEntity implements IInput, InputCallback {
	GameScreen curScreen = ScreenManager.getInstance().getCurScreen();
	Vector2 tmp = new Vector2();

	public RectTouchableEntity(Rectangle rectangle) {
		super(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
		curScreen.getImp().addProcessor(getInputListener());
	}

	public RectTouchableEntity(Vector2 position, Vector2 size) {
		super(position.x, position.y, size);
		curScreen.getImp().addProcessor(getInputListener());
	}

	public RectTouchableEntity(float x, float y, Vector2 size) {
		super(x, y, size.x, size.y);
		curScreen.getImp().addProcessor(getInputListener());
	}

	public RectTouchableEntity(float x, float y, float width, float height) {
		super(x, y, width, height);
		curScreen.getImp().addProcessor(getInputListener());
	}

	@Override
	public InputProcessor getInputListener() {
		// 实现只在实际触摸到矩形实体时才调用的回调函数
		return new InputAdapter() {
			@Override
			public boolean touchDown(int screenX, int screenY, int pointer, int button) {
				tmp.set(screenX,screenY);
				tmp = curScreen.getViewport().unproject(tmp);
				if (getRect().contains(tmp.x, tmp.y))
					onTouchDown((int) tmp.x, (int) tmp.y);
				return false;
			}

			@Override
			public boolean touchUp(int screenX, int screenY, int pointer, int button) {
				tmp.set(screenX,screenY);
				tmp = curScreen.getViewport().unproject(tmp);
				if (getRect().contains(tmp.x, tmp.y))
					onTouchUp((int) tmp.x, (int) tmp.y);
				return false;
			}

			@Override
			public boolean mouseMoved(int screenX, int screenY) {
				tmp.set(screenX,screenY);
				tmp = curScreen.getViewport().unproject(tmp);
				if (getRect().contains(tmp.x, tmp.y))
					onOverlap((int) tmp.x, (int) tmp.y);
				return false;
			}
		};
	}
}
